#coding=utf-8
#!/usr/bin/python


# cd /d D:\python2.6.6\dev_test 
#     cd /d D:\python26 
#     python.exe D:\py\demo\updb.py

""" 
业务操作组件   包含 inc_mod  数据模块
    
"""
import os, sys, string
import db_mysql as me_db   #包含文件不用后缀名 引用别  （命名空间）


class  tmp_tab():
    

    def init_ws(self): 
        "构建"
        pass
        self.db_ws = me_db.db_cls()
        self.db_ws.init('china.worldscrap.com','root','woshiug')
        self.db_ws.do_sql('use wsnew ');
        self.db_ws.do_sql(' set names utf8 ');
        
    def init_v2(self): 
        "构建"
        pass
        self.db_v2 = me_db.sql_cls()
        self.db_v2.init('192.168.0.249','root','0air.com')
        self.db_v2.do_sql('use sms_v2test  ');
        self.db_v2.do_sql(' set names utf8 ');
    
#------------------------------------------ insert v2
    def test(self):
        sql = ' show tables '
        rows = self.db_v2.get_all(sql)
        return rows
        
    def exist_cur_record(self,cid):
        "查看联系记入 "
        sql = u' SELECT t.* FROM sms_contact_record t WHERE t.customer_id  =   ' +str(cid)+ ' limit 5  '
        row = self.db_v2.get_all(sql)
        if self.db_v2.rowcount > 0:
            return True
        else:
            return False        


    def get_cur_by_phone(self,phone):
        "通过手机 获取客户"
        sql = u' select t.* from sms_customers t  where phone_number =  "' +str(phone)+ '"  or phone_number2 = "'+str(phone)+ '" limit 1 '
        row = self.db_v2.get_one(sql)
        return row

        
    def into_v2_cut(self,cut_dict):
        "插入数据 到v2 系统"
        
        sql = u'insert into sms_customers  '
        sql += u" ( phone_number,realname, email,phone,fax,address,company_name,area,remark,sales_id) values  "
        sql += u" ( "
        sql += "'" +self.get_dictv('手机号',cut_dict) +"','" + self.get_dictv('联系人',cut_dict) +"','" + self.get_dictv('Email',cut_dict) +"','"
        sql += self.get_dictv('联系电话',cut_dict) +"','" +self.get_dictv('传真',cut_dict) +"','" + self.get_dictv('联系地址',cut_dict) + "','" 
        sql += self.get_dictv('公司名称',cut_dict)  + "','" + self.get_dictv('所在区域',cut_dict) + "','" + self.get_dictv('备注',cut_dict) + "'" 
        sql += ", 123456789" 
        sql += u"  ) "
        state = self.db_v2.do_sql(sql)
    
        return state
        
    def up_v2_cur(self,cid,oth={}):
        "更新 客户数据 销售id "
        sql = u" UPDATE sms_customers t SET t.sales_id = 123456789 WHERE t.customer_id = "+ str(cid) +" limit 1 "
        state = self.db_v2.do_sql(sql)
        
        #客户备注为空
        tmp_row = self.get_cur_by_phone(self.get_dictv('phone_number',oth))
        tmp_remark = tmp_row['remark']
        if tmp_remark is None  or tmp_remark=='':
            sql = u" UPDATE sms_customers t SET t.remark  = '"+ oth['remark'] +"' WHERE t.customer_id = "+ str(cid) +" limit 1 "
            self.db_v2.do_sql(sql)
        
        return state


    def get_v2_insert_id(self):
        "获取 插入id"
        return self.db_v2.get_insert_id()
        
    
    
    def into_ws_to_v2(self,ws_id,v2_id):
        " 记入关系 联系"
        row = self.get_ws_to_v2(ws_id) 
        if row.has_key('wsid'):
            return 1
        #else:
        #    print 'do insert'
        sql = " INSERT INTO ws_to_v2 ( wsid,v2id ) VALUES ( "+ str(ws_id) + " , "+  str(v2_id) +" ) "
        state = self.db_v2.do_sql(sql)
        #if state:
        #    print 'if',state
        #else:
        #    print 'else',state
        return state
        
    def get_ws_to_v2(self,ws_id):
        " 获取 关系记入"
        sql = " SELECT t.* FROM ws_to_v2 t WHERE t.wsid = "+ str(ws_id) +"  limit 1"
        row = self.db_v2.get_one(sql)
        return row

#------------------------------------------ update ws
    def get_tmp_tab_all(self):
        "获取所有数据"
        row = self.db_ws.get_all(" SELECT t.* FROM tmp_table t   ")#limit 11
        return row
    def get_group_by_id(self,id):
        "ws_goodsgroups 获取分类名 通过id"
        row = self.db_ws.get_one(" SELECT t.* FROM ws_goodsgroups t  where id=" + str(id)  + "  limit 1 ")
        return row
    def get_group_all_dic(self):
        "ws_goodsgroups 所有分类字典"
        row = self.db_ws.get_all(" SELECT t.* FROM ws_goodsgroups t  ")
        agr_k = []
        arg_v = []
        agr_k = [ i['groupid'] for i in row] 
        agr_v = [ i['schinese'] for i in row]   
        ret_dic = dict(zip(agr_k,agr_v))
        return ret_dic
        
    def get_group_by_gid(self,gid):
        "ws_goodsgroups 获取分类名 通过id"
        row = self.db_ws.get_one(" SELECT t.* FROM ws_goodsgroups t  where groupid=\"" + str(gid)  + "\"  limit 1 ")
        return row

    def get_users_info_by_id(self,uid):
        "ws_users_info 客户资料 通过id"
        row = self.db_ws.get_one(" SELECT t.* FROM ws_users_info t  where uid=" + str(uid)  + "  limit 1 ")
        return row
    def get_mainattention_str(self,uid):
        " 经营范围  1废塑料  2废金属 3废纸  4机械  5其它   AA,AB"
        row = self.get_users_info_by_id(uid)
        return row
    def get_zyfx_str(self,val):
        " 主营方向 *  1销售  2采购  3两者都有  zyfx "
        if val == 1 :
            return '销售'
        if val == 2:
            return '采购'
        return '两者都有'
    def get_sell_group_all(self,uid):
        row = self.db_ws.get_all(" SELECT t.* FROM ws_cn_sell t  WHERE t.uid = " + str(uid) +" ORDER BY t.puttime DESC limit 20  ")
        ret_list = ''
        ret_list = [ i['groupid'] for i in row]  
        
        return ret_list
    def get_buy_group_all(self,uid):
        row = self.db_ws.get_all(" SELECT t.* FROM ws_cn_buy t  WHERE t.uid = " + str(uid) +" ORDER BY  t.time DESC limit 20  ")
        ret_list = ''
        ret_list = [ i['groupid'] for i in row]  
        
        return ret_list
    def get_product_group_all(self,uid):
        row = self.db_ws.get_all(" SELECT t.* FROM ws_cn_product t  WHERE t.uid = " + str(uid) +" ORDER BY  t.puttime DESC limit 20  ")
        ret_list = ''
        ret_list = [ i['groupid'] for i in row]  
        
        return ret_list

    def get_ws_insert_id(self):
        return self.db_ws.get_insert_id()

    def up_row(self,id,arg_str):
        
        sql = u" UPDATE   tmp_table t SET t.备注 = '" + arg_str  + u"' WHERE t.id = " + str(id).decode('utf8')  + u" LIMIT 1  "
        state = self.db_ws.do_sql(sql)
    
        return state
    def get_dictv(self,key,dict):
        if dict.has_key(key):
            if   dict[key]  is None :
                return u''
            if not isinstance( dict[key], unicode):
                return u''
            try:    
                dict[key].encode('gbk')
            except Exception, e:
                return u''
            return  dict[key]
        else:
            return u''



if __name__ == '__main__':
    print 'inc_com'
    sys.exit()
    




